import { defineStore } from "pinia";
// import router from "@/router";
// import { ElNotification } from "element-plus";

type Notice = {
    id: string;
    date: number;
    title: string;
    state: number; // 0：未读、1：已读、2：忽略、3：已处理、4：延迟处理
    type?: "info" | "warn" | "event" | "error" | "performance" | "equipAlarm";
    message?: string;
    data?: { fid?: string };
};

export const useNotification = defineStore("notification", {
    state() {
        return {
            notices: [] as Notice[],
        };
    },
    actions: {
        setNoticeState(id: string, value: number) {
            const notice = this.notices.find((v) => v.id == id);
            if (notice) {
                notice.state = value;
                localStorage.setItem("notices", JSON.stringify(this.notices));
            }
        },
        addNotice(notice: Notice, isSend: boolean = false) {
            this.notices.unshift(notice);
            isSend && this.sendNotice(notice.id);
        },
        sendNotice(id: string) {
            const notice = this.notices.find((v) => v.id == id);
            if (notice) {
                setTimeout(() => {
                    // if (Notification.permission == "granted") {
                    //     new Notification(notice.title, {
                    //         icon: "https://www.juyingiot.com/jycloud/static/img/ab00.9353939b.png",
                    //         body: notice.message,
                    //     }).onclick = focus;
                    // }
                    // ElNotification({
                    //     key: id,
                    //     title: notice.title,
                    //     message: notice.message || "",
                    //     type: notice.type == "equipAlarm" ? "warning" : notice.type == "info" ? "info" : "info",
                    //     onClick: focus,
                    // });
                }, 0);
                // function focus() {
                //     if (notice) {
                //         window.focus();
                //         switch (notice.type) {
                //             case "equipAlarm":
                //                 notice.state = 1;
                //                 router.replace("/?id=" + notice.data!.fid);
                //                 break;
                //         }
                //     }
                // }
            }
        },
    },
});
